//
//  CustomCalloutView.m
//  Tencent_Map_Demo_Vector
//
//  Created by WangXiaokun on 16/1/21.
//  Copyright © 2016年 WangXiaokun. All rights reserved.
//

#import "CustomCalloutView.h"

#define kArrowHeight 5


#define kMarginLeft 20
#define kMarginTop 3
#define kMaxTitleWidth 180.f
#define kContentHeight 37.f
@interface CustomCalloutView()


@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) UILabel *labelSubtitle;
@property (nonatomic, strong) UIStackView *stackView;

@end

@implementation CustomCalloutView


+ (instancetype)calloutViweWith:(NSString *)title subtitle:(NSString *)subtitle{
    
    
    CGSize titlesSize = [self sizeWith:title subtitle:subtitle];
    
    
    CustomCalloutView *customCalloutView = [[CustomCalloutView alloc]
                          initWithFrame:CGRectMake(0,
                                                   0,
                                                   titlesSize.width + kMarginLeft * 2 ,
                                                   kContentHeight + kArrowHeight)];
    customCalloutView.backgroundColor = [UIColor clearColor];
    [customCalloutView setTitle:title];
    [customCalloutView setSubtitle:subtitle];
    
    return customCalloutView;
}


+ (CGSize)sizeWith:(NSString*)title subtitle:(NSString*)subtitle{
    // 设定最大宽高
    CGSize size = CGSizeMake(kMaxTitleWidth, 30);
    CGRect frameTitle = [title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[self titleAttr] context:nil];
    CGRect frameSubtitle = [subtitle boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[self subtitleAttr] context:nil];
    
    CGFloat width = frameTitle.size.width > frameSubtitle.size.width ? frameTitle.size.width : frameSubtitle.size.width ;
    
    return CGSizeMake(width+2, frameTitle.size.height + frameSubtitle.size.height);
}


+(NSDictionary*)titleAttr{
    return @{NSFontAttributeName:kBoldFont(12),NSForegroundColorAttributeName:[UIColor whiteColor]};
}
+(NSDictionary*)subtitleAttr{
    return @{NSFontAttributeName:kFont(10),NSForegroundColorAttributeName:[UIColor whiteColor]};
}

#pragma mark - setData

-(void)setTitle:(NSString *)title {
    _labelTitle.attributedText = [[NSAttributedString alloc] initWithString:title
                                                                 attributes:[self.class titleAttr]];
    [_labelTitle sizeToFit ];
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kMarginTop);
        make.left.mas_equalTo(kMarginLeft);
        make.right.mas_equalTo(-kMarginLeft);
    }];
}

-(void)setSubtitle:(NSString *)subtitle {
    
    _labelSubtitle.attributedText = [[NSAttributedString alloc] initWithString:subtitle
                                                                    attributes:[self.class subtitleAttr]];
    [_labelSubtitle sizeToFit ];
    __weak typeof(self) weakSelf = self;
    [_labelSubtitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.labelTitle.mas_bottom).offset(2);
        make.left.mas_equalTo(kMarginLeft);
        make.right.mas_equalTo(-kMarginLeft);
    }];
}



#pragma mark - initFrame

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    
    _labelTitle = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:_labelTitle];
    _labelSubtitle = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:_labelSubtitle];
 
}

#pragma mark - draw view path
- (void)drawRect:(CGRect)rect {
    [self drawInContext:UIGraphicsGetCurrentContext()];
}

- (void)drawInContext:(CGContextRef) context {
    CGContextSetFillColorWithColor(context, LLColorRGB(30, 27, 40).CGColor);
    [self getDarwPath:context];
    CGContextFillPath(context);
}

- (void)getDarwPath:(CGContextRef) context {
    CGRect rect = self.bounds;
    CGFloat radius = (rect.size.height - kArrowHeight)/2.0;
    CGFloat minx = CGRectGetMinX(rect),
    midx = CGRectGetMidX(rect),
    maxx = CGRectGetMaxX(rect);
    CGFloat miny = CGRectGetMinY(rect),
    maxy = CGRectGetMaxY(rect) - kArrowHeight;
    //绘制三角形
    CGContextMoveToPoint(context, midx + kArrowHeight, maxy);
    CGContextAddLineToPoint(context, midx, maxy + kArrowHeight);
    CGContextAddLineToPoint(context, midx - kArrowHeight, maxy);
    //绘制圆角矩形
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, miny, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, minx, maxy, radius);
    
    CGContextClosePath(context);
}


@end
